package com.src.xyzk_personal.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.src.xyzk_personal.EOL.CheryMesInfo;
import com.src.xyzk_personal.EOL.EcuInfo;

import android.util.Log;
import android.util.Xml;

public class CommonXml {

	public static CheryMesInfo GetEcuInfoxml(String Pxmlstr)
	{
		CheryMesInfo mesinfo = new CheryMesInfo();
		List<EcuInfo> ecuinfolist = new ArrayList<EcuInfo>();
		//获取XML文件
		//String xmlstr = HttpRequest.sendRequestWithHttpURLConnection(new String(Common.cartype.vin),Common.Mes_version + "/Get_Electric");
		//XmlPullParserFactory factory = null;
		XmlPullParser xmlPullParser = null;
		if(Pxmlstr != null) //ok
		{
			try {
				//factory = XmlPullParserFactory.newInstance();
				//xmlPullParser = factory.newPullParser();
				xmlPullParser = Xml.newPullParser();
				//设置输入的内容
				xmlPullParser.setInput(new StringReader(Pxmlstr));
				// 得到当前事件类型
				int eventType = xmlPullParser.getEventType();
				EcuInfo ecuinfo = null;
				String name = null;
				while (eventType != XmlPullParser.END_DOCUMENT)
				{
					switch (eventType) {
						case XmlPullParser.START_DOCUMENT:
							break;
						case XmlPullParser.START_TAG:
							//获取名称
							name = xmlPullParser.getName();
							if ("ECU".equals(name))
							{
								ecuinfo = new EcuInfo();
								String v_t1 = xmlPullParser.getAttributeValue(null, "name");
								//String v_t2 = xmlPullParser.getAttributeName(0);
								ecuinfo.setname(v_t1);
							}
							else if("Parameter".equals(name))
							{
								if("Partnumber".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.setsn(vx);
								}
								else if("HARDWARE1".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.sethardver1(vx);
								}
								else if("HARDWARE2".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.sethardver2(vx);
								}
								else if("SOFTWARE1".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver1(vx);
								}
								else if("SOFTWARE2".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver2(vx);
								}
								//ISG/TM特殊处理
								else if("ISG_Offset_angle".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.sethardver1(vx);
								}
								else if("ISG_Offset_angle_direction".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.sethardver2(vx);
								}
								else if("TM_Offset_angle".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver1(vx);
								}
								else if("TM_Offset_angle_direction".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver2(vx);
								}
								else if("DJSN".equals(xmlPullParser.getAttributeValue(null, "name"))) //DJSN
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									mesinfo.DJSN = vx;
								}
								else if("GBSN".equals(xmlPullParser.getAttributeValue(null, "name"))) //DJSN
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);
										vx = new String(vbyte);
									}
									mesinfo.GBSN = vx;
								}
							}
							else if(name.equals("VinCode"))
							{
								String v_text = xmlPullParser.nextText();
								mesinfo.setvin(v_text);
							}
							else if(name.equals("MaterialCode"))
							{
								mesinfo.setMaterialCode(xmlPullParser.nextText());
							}
							else if(name.equals("CarType"))
							{
								mesinfo.setCarType(xmlPullParser.nextText());
							}
							else if(name.equals("Soft_Code"))
							{
								mesinfo.setSoftCode(xmlPullParser.nextText());
							}
							else if(name.equals("Status"))
							{
								mesinfo.setStatus(xmlPullParser.nextText());
							}
							else if(name.equals("FCM"))
							{
								mesinfo.flash_fcm = xmlPullParser.nextText();
							}
							else if(name.equals("FRM"))
							{
								mesinfo.flash_frm = xmlPullParser.nextText();
							}
							else if(name.equals("TCU"))
							{
								mesinfo.flash_tcu = xmlPullParser.nextText();
							}
							else if(name.equals("EMS1"))
							{
								mesinfo.flash_ems = xmlPullParser.nextText();
							}
							else if(name.equals("ESB"))
							{
								mesinfo.esp_code = xmlPullParser.nextText();
							}


							break;
						case XmlPullParser.END_TAG:
							name = xmlPullParser.getName();
							if("ECU".equals(name))
							{
								if(ecuinfo != null) //保存
								{
									ecuinfolist.add(ecuinfo);
									ecuinfo = null;
								}
							}
							break;
						case XmlPullParser.TEXT:
							break;
					}
					eventType = xmlPullParser.next();
				}


			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		mesinfo.setEcuInfo(ecuinfolist);
		return mesinfo;
	}
	public static CheryMesInfo GetEcuInfoMyjson(String Pmystr)
	{
		CheryMesInfo mesinfo = new CheryMesInfo();
		List<EcuInfo> ecuinfolist = new ArrayList<EcuInfo>();
		EcuInfo ecuinfo = null;
		int v_ad = 0;
		int v_end = Pmystr.indexOf(", ");
		while(v_end > 0) //ok,遍历json
		{
			String v_str = Pmystr.substring(0,v_end);
			if(v_str.indexOf("{") >= 0)
			{
				ecuinfo = new EcuInfo();
				String ecustr = v_str.substring(1,v_str.length() - 1);
				v_ad = ecustr.indexOf(",");
				while(v_ad > 0)
				{
					String items = ecustr.substring(0, v_ad);
					v_end = items.indexOf("=");
					if(v_end > 0)
					{
						String name = items.substring(0,v_end);
						String item = items.substring(v_end + 1);
						if("name".equals(name))
						{
							ecuinfo.setname(item);
						}
						else if("Partnumber".equals(name))
						{
							ecuinfo.setsn(item);
						}
						else if("HARDWARE1".equals(name))
						{
							ecuinfo.sethardver1(item);
						}
						else if("HARDWARE2".equals(name))
						{
							ecuinfo.sethardver2(item);
						}
						else if("SOFTWARE1".equals(name))
						{
							ecuinfo.setsoftver1(item);
						}
						else if("SOFTWARE2".equals(name))
						{
							ecuinfo.setsoftver2(item);
						}
					}
					if((v_ad + 1) < ecustr.length())
					{
						ecustr = ecustr.substring(v_ad + 1);
					}
					else
						break;
					if(ecustr != null && ecustr.length() < 5)
						break;
					v_ad = ecustr.indexOf(",");
					if(v_ad < 0)
					{
						if(ecustr.length() > 0)
							v_ad = ecustr.length();
					}
				}
				ecuinfolist.add(ecuinfo);
			}
			else if(v_str.indexOf("=") > 0)
			{
				v_ad = v_str.indexOf("=");
				String name = v_str.substring(0,v_ad);
				String item = v_str.substring(v_ad + 1);
				if("VinCode".equals(name))
				{
					mesinfo.setvin(item);
				}
				else if("MaterialCode".equals(name))
				{
					mesinfo.setMaterialCode(item);
				}
				else if("CarType".equals(name))
				{
					mesinfo.setCarType(item);
				}
				else if("Soft_Code".equals(name))
				{
					mesinfo.setSoftCode(item);
				}
			}
			if((v_end + 2) < Pmystr.length())
			{
				Pmystr = Pmystr.substring(v_end + 2);
				if(Pmystr.length() < 5)
					break;
			}
			else
				break;
			v_end = Pmystr.indexOf(", ");
			if(v_end < 0)
			{
				if(Pmystr.length() > 10)
					v_end = Pmystr.length();
			}
		}
		mesinfo.setEcuInfo(ecuinfolist);
		return mesinfo;
	}
	public static List<String>  GetEcuInfoSn(CheryMesInfo Pmesinfog,String Pname,String Psn)
	{
		Pmesinfog.setEcuId(-1);
		List<String> v_sn = new ArrayList<String>();
		if(Pmesinfog == null) return v_sn;
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		for(int i = 0; i < eculist.size(); i ++)
		{
			if(eculist.get(i).getname().equals(Pname))
			{
				v_sn.add(eculist.get(i).getsn());
				if(Psn.equals(eculist.get(i).getsn()))
					Pmesinfog.setEcuId(i); //缓存
			}
		}
		return v_sn;
	}
	public static String GetEcuInfoHard(CheryMesInfo Pmesinfog,String Pname)
	{
		String hard = "";
		if(Pmesinfog == null) return hard;
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		int lastItem = Pmesinfog.getEcuId();
		if((lastItem >= 0) && (Pname.equals(eculist.get(lastItem).getname()))) //直接取上次搜索结果
		{
			String hard1 = eculist.get(lastItem).gethardver1();
			String hard2 = eculist.get(lastItem).gethardver2();
			if(hard1 != null && hard1.length() > 0)
				hard = hard1;
			if(hard2 != null && hard2.length() > 0)
			{
				if(hard.length() > 0)
					hard += "/" + hard2;
			}
		}
		else
		{
			for(int i = 0; i < eculist.size(); i ++)
			{
				if(eculist.get(i).getname().equals(Pname))
				{
					String hard1 = eculist.get(i).gethardver1();
					String hard2 = eculist.get(i).gethardver2();
					if(hard1 != null && hard1.length() > 0)
						hard = hard1;
					if(hard2 != null && hard2.length() > 0)
					{
						if(hard.length() > 0)
							hard += "/" + hard2;
					}
					Pmesinfog.setEcuId(i); //缓存
				}
			}
		}
		return hard;
	}
	public static String GetEcuInfoSoft(CheryMesInfo Pmesinfog,String Pname)
	{
		String soft = "";
		if(Pmesinfog == null) return soft;
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		int lastItem = Pmesinfog.getEcuId();
		if((lastItem >= 0) && (Pname.equals(eculist.get(lastItem).getname()))) //直接取上次搜索结果
		{
			String soft1 = eculist.get(lastItem).getsoftver1();
			String soft2 = eculist.get(lastItem).getsoftver2();
			if(soft1 != null && soft1.length() > 0)
				soft = soft1;
			if(soft2 != null && soft2.length() > 0)
			{
				if(soft.length() > 0)
					soft += "/" + soft2;
			}
		}
		else
		{
			for(int i = 0; i < eculist.size(); i ++)
			{
				if(eculist.get(i).getname().equals(Pname))
				{
					String soft1 = eculist.get(i).getsoftver1();
					String soft2 = eculist.get(i).getsoftver2();
					if(soft1 != null && soft1.length() > 0)
						soft = soft1;
					if(soft2 != null && soft2.length() > 0)
					{
						if(soft.length() > 0)
							soft += "/" + soft2;
					}
					Pmesinfog.setEcuId(i); //缓存
				}
			}
		}
		return soft;
	}
	public static void Cheanfocus(byte [] Pdata)
	{
		for(int i = 0; i < Pdata.length; i ++)
		{
			if(Pdata[i] == ' ')
				Pdata[i] = 0x00;
		}
	}
	//判断Pone是否跟Pc1 or Pc2 相同
	public static boolean StingCompare(String Pone,String Pc1,String Pc2)
	{
		boolean isok = false;
		//String v_one;
		int len1 = Pone.length();
		int len2 = 0;
		if(Pc1 != null)
			len2 = Pc1.length();
		int len3 = 0;
		if(Pc2 != null)
			len3 = Pc2.length();
		//int len = len1;
		if(len1 <= 0)
			isok = true;
		else if(len2 > 0)
		{
			//if(len > len2)
			//	len = len2;
			//v_one = Pone.substring(0, len);
			if(Pone.equals(Pc1))
				isok = true;
			else if(len3 > 0)
			{
				//if(len > len3)
				//	len = len3;
				//v_one = Pone.substring(0, len);
				if(Pone.equals(Pc2))
					isok = true;
			}
		}
		else if(len3 > 0)
		{
			//if(len > len3)
			//	len = len3;
			//v_one = Pone.substring(0, len);
			if(Pone.equals(Pc2))
				isok = true;
		}
		return isok;
	}
	public static byte [] ReadFrmXmlFile(String FileName)
	{
		byte [] readbyte = null;
		String v_read = "";
		int readlen = 0;
		try {

			FileInputStream fileInputStream = new FileInputStream(FileName);
			long filelength = fileInputStream.available();
			if(filelength <= 0) return readbyte;
			byte [] readbuf = new byte[(int) (filelength)];
			readlen = fileInputStream.read(readbuf);
			String vstr = new String(readbuf);
			int v_start = vstr.indexOf("<DATEN>");
			int v_end = vstr.indexOf("</DATEN>");
			if(v_start > 0 && v_end > v_start)
				v_read = vstr.substring(v_start + 7,v_end);
			//转换
			readbyte = new byte[v_read.length() / 2];
			Commonfunc.StringToBytes(v_read, readbyte, readbyte.length);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return readbyte;
	}
	public static CheryMesInfo GetEcuInfofromECOS(String PJsonstr) throws JSONException {
		Log.i("TAG", "GetEcuInfofromECOS: " + PJsonstr);
		CheryMesInfo mesinfo = new CheryMesInfo();
		List<EcuInfo> ecuinfolist = new ArrayList<EcuInfo>();
		//获取XML文件
		//String xmlstr = HttpRequest.sendRequestWithHttpURLConnection(new String(Common.cartype.vin),Common.Mes_version + "/Get_Electric");
		//XmlPullParserFactory factory = null;
		JSONObject obj = new JSONObject(PJsonstr);
		if(obj == null) return mesinfo;
		JSONObject objdata = new JSONObject(obj.getString("data"));
		if(objdata == null) return mesinfo;

		//mesinfo.setvin(new String(cartype.vin));
		mesinfo.setMaterialCode(objdata.getString("modelName"));
		mesinfo.setCarType(objdata.getString("bodyColor"));
		mesinfo.setSoftCode(objdata.getString("esk"));
		//特殊处理
		mesinfo.flash_ems = objdata.getString("vin");

		//PINcode
		mesinfo.PinCode = objdata.getString("pin");
		//mesinfo.emsfile = objdata.getString("info");

		JSONArray ObjecuList = new JSONArray(objdata.getString("tableModuleEcuarrays"));
		int nums = ObjecuList.length();
		EcuInfo ecuinfo = null;
		for(int i =0; i < nums; i ++) {
			String ss = ObjecuList.get(i).toString();
			JSONObject ObjSon = new JSONObject(ss);
			ecuinfo = new EcuInfo();
			ecuinfo.setname(ObjSon.getString("ecuName"));
			ecuinfo.setsn(ObjSon.getString("partNumber"));
			ecuinfo.sethardver1(ObjSon.getString("errorCode"));
			ecuinfo.sethardver2(ObjSon.getString("hardwareVersion"));
			//ecuinfo.sethardver3("");
			ecuinfo.setsoftver1(ObjSon.getString("softwareAss"));
			ecuinfo.setsoftver2(ObjSon.getString("softwareVersion"));
			//ecuinfo.setsoftver3("");
			ecuinfolist.add(ecuinfo);
			if("TCU".equals(ecuinfo.getname()))
			{
				mesinfo.flash_tcu = ecuinfo.getsoftver1();
				mesinfo.setTCU_SN(ecuinfo.getsn());
			}
			else if("EMS".equals(ecuinfo.getname()))
			{
				mesinfo.flash_ems = ecuinfo.getsoftver1();
			}
		}
		mesinfo.setEcuInfo(ecuinfolist);
		Log.i("TAG", "GetEcuInfofromECOS: " + mesinfo);
		return mesinfo;
	}
}
